# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:         
    
        def cntTree(node):
            if node is None:
                return 0
            l = cntTree(node.left)
            if l == -1:
                return -1
            r = cntTree(node.right)
            if r == -1 or abs(l-r) >1:
                return -1
            return max(l,r)+1
        return cntTree(root) != -1